﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

namespace SMS.Admin.Modules
{
    public partial class DetailService : WebControlBase
    {
        private long ServiceID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            ServiceID = Request.QueryString["serviceId"] != null ? Int32.Parse(Request.QueryString["serviceId"].ToString().Trim()) : -1;

            if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1 && ClientState.MemberInfo.UserType != 2)
            {
                if (ServiceID == -1)
                    lnkAdd.Enabled = false;
                else
                    lnkAdd.Enabled = true;
            }

            if (this.IsPostBack) return;

            if (ServiceID > 0)
            {
                this.BindObject();
            }
        }

        private void BindObject()
        {
            if (ServiceID != -1)
            {
                ServicesInfo objService = ServicesManager.Select(ServiceID);
                if (objService != null)
                {
                    txtServiceBackup.Text = objService.ServiceBackup;
                    txtServiceIndex.Text = objService.ServiceIndex.ToString();
                    txtServiceName.Text = objService.ServiceName;
                }
            }
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (ServiceID != -1)
                {
                    ServicesInfo objService = ServicesManager.Select(ServiceID);
                    if (objService != null)
                    {
                        objService.ServiceName = txtServiceName.Text;
                        objService.ServiceBackup = txtServiceBackup.Text;
                        objService.ServiceIndex = byte.Parse(txtServiceIndex.Text.Trim());
                        bool result = ServicesManager.Update(objService);
                        if (result)
                            lblError.Text = "Cập nhật thành công!";
                        else
                            lblError.Text = "Cập nhật lỗi!";
                    }
                }
                else
                {
                    ServicesInfo objService = new ServicesInfo();
                    objService.ServiceName = txtServiceName.Text;
                    objService.ServiceBackup = txtServiceBackup.Text;
                    objService.ServiceIndex = byte.Parse(txtServiceIndex.Text.Trim());
                    bool result = ServicesManager.Insert(objService);
                    if (result)
                        lblError.Text = "Thêm mới thành công!";
                    else
                        lblError.Text = "Thêm mới lỗi!";
                }
            }
            catch { }
        }
    }
}